﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class Add_Module : BasePage
    {
        private Sys_ModuleBLL smb = new Sys_ModuleBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dtmg = (DataTable)Session["ModuleGroup"];
                ModuleGroup.DataSource = dtmg;
                ModuleGroup.DataTextField = "NAME";
                ModuleGroup.DataValueField = "ID";
                ModuleGroup.DataBind();
                ModuleGroup.SelectedValue = "-1";

                BindModule();
                BindModuleQueue();
            }
            if (Stutar.Value.ToString().Trim() == "Add_Module")
            {
                Add();
                Stutar.Value = "";
            }

            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();
        }

        private void setList(System.Web.UI.WebControls.DropDownList lb)
        {
            DataTable dataCompany = smb.GetAllInfo(ModuleGroup.SelectedValue.ToString());
            int idx = -1;

            for (int n = 0; n < dataCompany.Rows.Count; n++)
            {
                if (dataCompany.Rows[n]["MODULE_PARENTID"].ToString() == ModuleGroup.SelectedValue.ToString())
                {
                    idx = idx + 1;
                    addItem(lb, dataCompany.Rows[n]["MODULE_ID"].ToString(), dataCompany.Rows[n]["MODULE_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        private void addItem(System.Web.UI.WebControls.DropDownList lb, String sId, String sName, int idx)
        {
            String sn = sName;
            for (int n = 0; n < idx; n++)
            {
                sn = (char)58853 + "" + sn;
            }
            System.Web.UI.WebControls.ListItem it = new ListItem(sn, sId);

            lb.Items.Add(it);
            DataTable dtCompany = smb.GetAllInfo(ModuleGroup.SelectedValue.ToString());
            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["MODULE_PARENTID"].ToString() == sId)
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["MODULE_ID"].ToString(), dtCompany.Rows[n]["MODULE_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        private void BindModule()
        {
            ddlModule.Items.Clear();
            setList(ddlModule);
            ddlModule.Items.Insert(0, new ListItem("---请选择---", "-1"));
        }

        private void BindModuleQueue()
        {
            ListModuleQueue.Items.Clear();

            foreach (var VARIABLE in smb.GetFatherModules(ModuleGroup.SelectedValue.ToString()).AsEnumerable().ToList())
            {
                ListModuleQueue.Items.Add(VARIABLE["MODULE_NAME"].ToString());
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void Add()
        {
            Sys_Module model = new Sys_Module();
            if (string.IsNullOrEmpty(txtName.Value.ToString().Trim()))
            {
                MessageBox.Show(Page, "请输入模块名称");
                return;
            }
            if (string.IsNullOrEmpty(txtPath.Value.ToString().Trim()))
            {
                MessageBox.Show(Page, "请输入模块路径");
                return;
            }
            string orderByStr = "";
            model.MODULE_NAME = txtName.Value.ToString().Trim();
            if (ModuleParId.Value.ToString().Trim() == "Y")
            {
                orderByStr = smb.OrderByNum("");
                model.MODULE_PARENTID = decimal.Parse(ModuleGroup.SelectedValue.ToString());
                model.MODULE_ORDERNUM1 = decimal.Parse((Convert.ToInt32(orderByStr.Split('-')[0].ToString()) + 1).ToString());
                model.MODULE_ORDERNUM2 = 0;
                model.MODULE_PATH = ModuleGroup.SelectedValue.ToString();
            }
            else
            {
                Sys_Module m = smb.GetInfoById(ddlModule.SelectedValue);
                if (m.MODULE_PARENTID > 0)
                {
                    orderByStr = smb.OrderByNum(m.MODULE_PARENTID.ToString());
                    if (orderByStr != null && string.IsNullOrEmpty(orderByStr) && !string.IsNullOrEmpty(orderByStr))
                    {
                        model.MODULE_ORDERNUM1 = decimal.Parse(orderByStr.Split('-')[0].ToString());
                        model.MODULE_ORDERNUM2 = decimal.Parse((Convert.ToInt32(orderByStr.Split('-')[1].ToString()) + 1).ToString());
                    }
                }
                else
                {
                    orderByStr = smb.OrderByNum(ddlModule.SelectedValue);
                    model.MODULE_ORDERNUM1 = decimal.Parse(orderByStr.Split('-')[0].ToString());
                    model.MODULE_ORDERNUM2 = decimal.Parse((Convert.ToInt32(orderByStr.Split('-')[1].ToString()) + 1).ToString());
                }
                model.MODULE_PARENTID = decimal.Parse(ddlModule.SelectedValue);
                model.MODULE_PATH = txtPath.Value.Trim();
            }
            model.MODULE_STATE = ddlState.SelectedValue;

            int count = smb.INSERT(model, lognUser.USER_NAME);
            if (count > 0)
            {
                Response.Write("<script>if(confirm('您是否继续添加?')){ location.href='Add_Module.aspx'; }else{ location.href='SelectModule.aspx';}</script>");
            }
            ModuleParId.Value = "";
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            int selectedIndex = ListModuleQueue.SelectedIndex;
            if (selectedIndex > 0)
            {
                string saveItemName = ListModuleQueue.SelectedValue;
                ListModuleQueue.Items.RemoveAt(selectedIndex);
                ListModuleQueue.Items.Insert(selectedIndex - 1, saveItemName);
                ListModuleQueue.SelectedValue = saveItemName;
            }
        }

        protected void btnDown_Click(object sender, EventArgs e)
        {
            int selectedIndex = ListModuleQueue.SelectedIndex;
            if (selectedIndex < ListModuleQueue.Items.Count - 1 && selectedIndex >= 0)
            {
                string saveItemName = ListModuleQueue.SelectedValue;
                ListModuleQueue.Items.RemoveAt(selectedIndex);
                ListModuleQueue.Items.Insert(selectedIndex + 1, saveItemName);
                ListModuleQueue.SelectedValue = saveItemName;
            }
        }

        protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModule.SelectedValue == "-1")
            {
                int mPId = Convert.ToInt32(smb
                    .GetFatherIdByName(ddlModule.SelectedValue)
                    .AsEnumerable()
                    .ToList()[0]);
                ListModuleQueue.Items.Insert(0, txtName.Value.Trim());
            }
        }

        protected void ModuleGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindModule();
            BindModuleQueue();
        }
    }
}